package com.lihaichen.utils.soap.web2;

import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lihaichen.utils.soap.SoapHelper;

/**
 * This class is to add 
 * 
 * <p>It supports <UL><LI>CustomerListRequest operation</LI></UL></p>
 * 
 * @author  Richard Li
 * @since   May 2012
 * @version 1.0
 * 
 *
 */
public class Web2Helper extends SoapHelper{

	private static final Logger logger = LoggerFactory.getLogger(Web2Helper.class.getName());
	
	/* 
	 * the default values as follows will be overridden by using the value in make.xml in runtime
	 * 
	 */
	private static final String DEFAULT_NAMESPACE = "http://sample10.datapower.ibm.com/crafted/";
	private static final String DEFAULT_PREFIX = "q0";
	private static final String DEFAULT_URLLINK = "http://127.0.0.1:8080/SampleRead/services/craftedSOAP";
	private static final String DEFAULT_WEBENDPOINT = "";        
    	
	//======================================================================
	// constructors as follows:
	//======================================================================
		
	public Web2Helper() {
                super.isSOAP12=false;
		setPrefix(DEFAULT_PREFIX);
		setNameSpace(DEFAULT_NAMESPACE);
		setUrlLink(DEFAULT_URLLINK);
		setWebEndpoint(DEFAULT_WEBENDPOINT);
	}

	//======================================================================
	// main methods as follows:
	//======================================================================
		
        public SOAPMessage callWS(String urlLink, int length) throws SOAPException{
		setUrlLink(urlLink);
		return callCustomerListRequest(length);
        }

	public SOAPMessage callCustomerListRequest(int length) throws SOAPException {
		return callWS(buildCustomerListRequest(length));
	}
	
	protected SOAPMessage buildCustomerListRequest(int length) {

		String operation = "CustomerListRequest";
		String soapAction = "ListAction";
		String elementLengthID = "length";
		
		SOAPMessage message = null;
		try {

			message = getSoapMessageTemplate(soapAction);
						
			// build body
			SOAPBodyElement bodyElement = message.getSOAPBody().addBodyElement(getQName(operation));
			addChildElementNoPrefix(length+"", elementLengthID, bodyElement);
			

		} catch (SOAPException e) {
			logger.error("Caught exception " , e);
		}
		return message;
	}
	
}
